package com.artPreter.controller.pc;

import com.artPreter.pojo.*;
import com.artPreter.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;

@Controller
public class ArticleController {

    @Autowired
    private IArticleService articleService;
    @Autowired
    private IArtistService artistService;
    @Autowired
    private IArtworkService artWorkService;
    @Autowired
    private IDepartService departService;
    @Autowired
    private IArtTypeService artTypeService;
    @Autowired
    private IDepartContentSectionService departContentSectionService;
    @Autowired
    private ISystemConfigService systemConfigService;

    @GetMapping("/article/{articleId}")
    public String toArticle(@PathVariable("articleId") String articleId, Model model) {
        // 头部菜单
        List<DepartContentSection> menuList = departContentSectionService.getMenuList();
        model.addAttribute("menus",menuList);

        // 首页显示艺术家
        List<Artist> indexArtists = artistService.getIndexArtists(32);
        model.addAttribute("indexArtists",indexArtists);

        // 首页显示艺术类型
        List<ArtType> artTypes = artTypeService.getList();
        model.addAttribute("artTypes",artTypes);


        List<Article> articles = articleService.getArticleList(articleId);
        if(articles.size()>0){
            Article article = articles.get(0);

            if(article.getLink()!=null&&!article.getLink().equals("")){
                // 文章链接
                String link = article.getLink();
                return "redirect:"+link;
            }



            model.addAttribute("article",article);
            return "pc/article";
        } else {
            return "redirect:/";
        }
    }

    /**
     * 通过id查询文章
     * @param id 文章id
     * @param
     * @param model
     * @return
     */
    @GetMapping("/article")
    public String toArticleById(Integer id,Integer cid, Model model) {

        // 系统设置
        HashMap systemConfig = systemConfigService.getSystemConfig();
        model.addAttribute("systemConfig",systemConfig);
        // 头部菜单
        List<DepartContentSection> menuList = departContentSectionService.getMenuList();
        model.addAttribute("menus",menuList);
        for (int i = 0; i < menuList.size(); i++) {
            if(menuList.get(i).getId().equals(cid)){
                menuList.get(i).setIsActice(1);
                model.addAttribute("backName",menuList.get(i).getName());
                System.out.println(menuList.get(i).getName());
            }
        }
        model.addAttribute("cid",cid);

        // 首页显示艺术家
        List<Artist> indexArtists = artistService.getIndexArtists(32);
        model.addAttribute("indexArtists",indexArtists);

        // 首页显示艺术类型
        List<ArtType> artTypes = artTypeService.getList();
        model.addAttribute("artTypes",artTypes);


        Article article = articleService.getArticleByNumberId(id);
        if(article!=null&&article.getId()!=null){
            model.addAttribute("article",article);
            return "mobile/index/article_detail";
        } else {
            return "redirect:/";
        }
    }
}
